﻿Public Class KhachHangDAO
    Private connect As Connect
    Private sqlstr As String
    Public Sub New()
        connect = New Connect
    End Sub

    Public Function CapNhat(ByVal emp As KhachHangDTO, ByVal flag As Boolean)
        Dim makh As String = emp.MaKH
        Dim maloainha As String = emp.MaLoaiNha
        Dim tenkh As String = emp.TenKH
        Dim diachi As String = emp.DiaChiKH
        Dim sodt As String = emp.SoDTKH
        Dim tgthue As String = emp.ThoiGianThue
        Dim nbd As String = emp.NgayBatDauThue
        Dim tienthue As String = emp.TienThue
        '-------------------------------------------'
        If flag = True Then
            sqlstr = "INSERT KHACHHANG (MaKH, MaLoaiNha,TenKH, DiaChiKH, SoDTKH,ThoiGianThue,NgayBatDauThue,TienThue)" & _
                        " VALUES('" & makh & "', '" & maloainha & "', '" & _
                                        tenkh & "', '" & diachi & "','" & sodt & "', '" & _
                                        tgthue & "', '" & nbd & "', '" & tienthue & "')"
        Else
            sqlstr = "UPDATE KHACHHANG SET  MaKH  = '" & makh & _
                                        "', MaLoaiNha   = '" & maloainha & _
                                        "', TenKH      = '" & tenkh & _
                                        "',  DiaChiKH      = '" & diachi & _
                                         "',  SoDTKH      = '" & sodt & _
                                          "',  ThoiGianThue      = '" & tgthue & _
                                           "',  NgayBatDauThue      = '" & nbd & _
                                             "',  TienThue      = '" & tienthue & _
                                    "' WHERE MaKH  = '" & makh & "'"

        End If
        Return connect.Execute(sqlstr)
    End Function
    Public Function GetData() As List(Of KhachHangDTO)
        Dim i As Integer
        Dim mkh, mln, tKH, dcKH, sdt, tgthue, ngbd, tthue As String
        Dim khDTO As KhachHangDTO
        Dim khList As New List(Of KhachHangDTO)
        Dim dt As New DataTable
        dt = connect.GetData("select * from KHACHHANG")
        For i = 0 To dt.Rows.Count() - 1
            mkh = dt.Rows(i)("MaKH").ToString
            mln = dt.Rows(i)("MaLoaiNha").ToString
            tKH = dt.Rows(i)("TenKH").ToString
            dcKH = dt.Rows(i)("DiaChiKH").ToString
            sdt = dt.Rows(i)("SoDTKH").ToString
            tgthue = dt.Rows(i)("ThoiGianThue").ToString
            ngbd = dt.Rows(i)("NgayBatDauThue").ToString
            tthue = dt.Rows(i)("TienThue").ToString
            khDTO = New KhachHangDTO(mkh, mln, tKH, dcKH, sdt, tgthue, ngbd, tthue)
            khList.Add(khDTO)
        Next
        Return khList
    End Function

    Public Function XoaKhachHang(ByVal emp As KhachHangDTO)
        Dim makh As String = emp.MaKH()
        sqlstr = "DELETE KHACHHANG WHERE MaKH = '" & makh & "'"
        Return connect.Execute(sqlstr)
    End Function
End Class
